from django.shortcuts import render, redirect, HttpResponse
from medical import models
from medical.utils.pagination import Pagination
from medical.utils.bootstrap import BootStrapModelForm
from django.core.exceptions import ValidationError


class Main_dep_addModelForm(BootStrapModelForm):
    class Meta:
        model = models.main_department
        fields = ["main_dep_id", "main_dep_name"]

    def clean_main_dep_name(self):
        txt_name = self.cleaned_data["main_dep_name"]
        # 验证次科室名称存在
        print(self.instance.pk)
        exists = models.main_department.objects.exclude(main_dep_id=self.instance.pk).filter(
            main_dep_name=txt_name).exists()
        if exists:
            raise ValidationError("该科室已经存在")
        return txt_name


class Main_dep_editModelForm(BootStrapModelForm):
    class Meta:
        model = models.main_department
        fields = ["main_dep_id", "main_dep_name"]

    def clean_main_dep_name(self):
        txt_name = self.cleaned_data["main_dep_name"]
        # 验证次科室存在
        print(self.instance.pk)
        exists = models.main_department.objects.exclude(main_dep_id=self.instance.pk).filter(
            main_dep_name=txt_name).exists()
        if exists:
            raise ValidationError("该主科室已经存在")
        return txt_name


# 医院表
def main_dep_list(request):
    """主科室列表"""
    # 构造搜索
    data_dict = {}
    search_data = request.GET.get('search', "")
    if search_data:
        data_dict["main_dep_name__contains"] = search_data

    queryset = models.main_department.objects.filter(**data_dict)

    # 分页
    page_object = Pagination(request, queryset, page_size=5)

    context = {
        'queryset': page_object.page_queryset,
        'page_string': page_object.html(),
        "search_data": search_data
    }

    return render(request, 'main_dep_list.html', context)


# forms.ModelForm 无样式
# # BootStrapModelForm 前端样式


def main_dep_add(request):
    """添加主科室表"""
    title = "添加主科室"
    if request.method == "GET":
        form = Main_dep_addModelForm()
        return render(request, 'template.html', {'form': form, "title": title})

    form = Main_dep_addModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('/main_dep/list/')

    return render(request, 'template.html', {'form': form, 'title': title})


def main_dep_edit(request, nid):
    """编辑主科室表"""
    title = "修改主科室"

    row_object = models.main_department.objects.filter(main_dep_id=nid).first()
    if not row_object:
        return redirect('/main_dep/list/')

    if request.method == "GET":
        form = Main_dep_editModelForm(instance=row_object)
        return render(request, 'template.html', {"form": form, "title": title})

    form = Main_dep_editModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return redirect('/main_dep/list/')
    return render(request, 'template.html', {"form": form, "title": title})


def main_dep_delete(request, nid):
    """删除主科室表"""
    models.main_department.objects.filter(main_dep_id=nid).delete()
    return redirect('/main_dep/list/')
